﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class PhieuMuaHangDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        public static List<PHIEUMUAHANG> GetAllPhieuMuaHang()
        {
            return db.PHIEUMUAHANGs.ToList();
        }
        public static List<PHIEUMUAHANG> SelectPhieuMuaHang()
        {
            return db.PHIEUMUAHANGs.ToList();
        }
        public static List<PHIEUMUAHANG> SelectPhieuMuaHangByNgayThanhToan(PHIEUMUAHANG pmh)
        {
            var query = from d in db.PHIEUMUAHANGs
                        where d.NgayThanhToan == d.NgayThanhToan
                        select d;

            return query.ToList();
        }
        public static void Insert(PHIEUMUAHANG pmh)
        {
            db.PHIEUMUAHANGs.Add(pmh);
            db.SaveChanges();
        }
        public static void Delete(PHIEUMUAHANG pmh)
        {
            var query = db.PHIEUMUAHANGs.Find(pmh.MaPhieuMuaHang);
            if (query != null)
            {
                db.PHIEUMUAHANGs.Remove(query);
                db.SaveChanges();
            }        
        }

        public static int GetLastMaPhieuMuaHang()
        {
            var query = (from d in db.PHIEUMUAHANGs
                        select d.MaPhieuMuaHang).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
